package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 护理计划分页查询
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingPlanVo> pageBean(NursingPlanPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<NursingPlanVo> list = nursingPlanMapper.pageQuery(dto);

        for (NursingPlanVo nursingPlanvo : list) {
            Integer i = nursingLevelMapper.getPlanCount(nursingPlanvo.getId());
            nursingPlanvo.setCount(i);
        }
        Page<NursingPlanVo> page = (Page<NursingPlanVo>) list;
        return PageBean.of(page,NursingPlanVo.class);

    }
    /**
     * 添加护理计划
     */
    @Override
    @Transactional
    public void add(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.add(nursingPlan);

        List<NursingProjectPlanDto> list1 = dto.getProjectPlans();
        List<NursingProjectPlan> list = new ArrayList<>();
        for (NursingProjectPlanDto nursingProjectPlanDto : list1) {
            nursingProjectPlanDto.setPlanId(nursingPlan.getId());
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class);
            list.add(nursingProjectPlan);
        }
        nursingProjectPlanMapper.addBitch(list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        return nursingPlanVo;
    }

    /**
     * 根据Id修改
     * @param dto
     */
    @Override
    @Transactional
    public void update(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);

        nursingProjectPlanMapper.deleteAll(dto.getId());
        List<NursingProjectPlan> collect = dto.getProjectPlans().stream()
                .map((Function<NursingProjectPlanDto, NursingProjectPlan>) nursingProjectPlanDto -> BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class))
                .collect(Collectors.toList());
        for (NursingProjectPlan nursingProjectPlan : collect) {
            nursingProjectPlan.setPlanId(nursingPlan.id);
        }
        nursingProjectPlanMapper.addBitch(collect);
    }
    /**
     * 删除护理计划
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
       NursingLevel nursingLevel = nursingLevelMapper.getByPlanId(id);
       if(nursingLevel == null){
           nursingPlanMapper.delete(id);
       }else {
           throw new BeanException("护理等级还有关联项,无法删除");
       }
    }
    /**
     * 启用,禁用护理计划
     */
    // TODO: 是否要根据护理水平来限制禁用启用?
    @Override
    public void startOrStop(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        if(nursingLevelMapper.getByPlanId(id)==null){
            nursingPlanMapper.update(nursingPlan);
        }else {
            throw new BeanException("护理等级还有关联项,无法禁用");
        }
    }
    /**
     * 查询所有护理计划
     */
    @Override
    public List<NursingPlanVo> getAllNP() {
        return nursingPlanMapper.getAllNP();
    }
}
